<?php
class DictionariesController extends AppController {
	
	var $name = 'Dictionaries';
	var $layout = 'admin';
	var $helpers = array('Javascript');
	
	public function index() {
		$params ['order'] = 'id';
		if (isset ( $this->params ['named'] ['orderBy'] )) {
			$params ['order'] = $this->params ['named'] ['orderBy'];
			$this->set ( 'orderBy', $this->params ['named'] ['orderBy'] );
			if (isset ( $this->params ['named'] ['DESC'] )) {
				$params ['order'] .= ' DESC';
				$this->set ( 'DESC', $this->params ['named'] ['DESC'] );
			}
		}
		$dictionaries = $this->Dictionary->find ( 'all', $params );
		$this->set ( 'dictionaries', $dictionaries );
	}
	
	public function delete($id = NULL) {
		if ($id) {
			if ($this->Dictionary->delete ( $id )) {
				$this->Session->setFlash ( 'Dictionary item deleted successfuly!' );
				$this->redirect ( array ('action' => 'index' ) );
			} else {
				$this->Session->setFlash ( 'Failed! Please try again or contact to administrator.' );
				$this->redirect ( array ('action' => 'index' ) );
			}
		
		} else {
			$this->Session->setFlash ( 'Failed! No such dictionary item.' );
			$this->redirect ( array ('action' => 'index' ) );
		
		}
	}
	
	public function add() {
		if (! empty ( $this->data )) {
			
			$saveable = array (	"key" => $this->data ['Dictionary'] ['key'] ,
								"eng" => $this->data ['Dictionary'] ['eng'] ,
			 					"rus" => $this->data ['Dictionary'] ['rus'] ,
								"arm" => $this->data ['Dictionary'] ['arm'] ,
								"group" => ($this->data ['Dictionary'] ['group'] == -2) ? $this->data['Dictionary']['group_other'] : $this->data ['Dictionary'] ['group'] );
			
			
			$this->Dictionary->create ();
			
			if ($this->Dictionary->save ( $saveable )) {
				$this->Session->setFlash ( 'Dictionary item added successfuly!' );
				$this->redirect ( array ('action' => 'index' ) );
			} else {
				$this->Session->setFlash ( 'Failed to add dictionary item!' );
			}
		} else {
			$allgroups = $this->Dictionary->find ( 'all', array ('fields' => 'DISTINCT group') );
			$groups ['-1'] = '--select--';
			$groups ['-2'] = '--other--';
			
			foreach ( $allgroups as $group ) {
				$groups [ $group ['Dictionary'] ['group'] ] = $group ['Dictionary'] ['group'];
			}
			$this->set ( 'groups', $groups );
		}
		$this->set('pageTitle', $this->pageTitle = 'Add dictionary item');
	}
	
	public function edit($id) {
		if (! empty ( $this->data )) {
			$this->Dictionary->id = $id;
			$this->data ['Dictionary'] ['group'] = ($this->data ['Dictionary'] ['group'] == -2) ? $this->data['Dictionary']['group_other'] : $this->data ['Dictionary'] ['group'];
			if ($this->Dictionary->save ( $this->data )) {
				$this->Session->setFlash ( 'Dictionary item edited successfuly!' );
				$this->redirect ( array ('action' => 'index' ) );
			} else {
				$this->Session->setFlash ( 'Failed to edit dictionary item!' );
			}
		} else {
			$allgroups = $this->Dictionary->find ( 'all', array ('fields' => 'DISTINCT group') );
			$groups ['-1'] = '--select--';
			$groups ['-2'] = '--other--';
			
			foreach ( $allgroups as $group ) {
				$groups [ $group ['Dictionary'] ['group'] ] = $group ['Dictionary'] ['group'];
			}
			$this->set ( 'groups', $groups );
			$this->data = $this->Dictionary->findById ( $id );
		}
		$this->set('pageTitle', $this->pageTitle = 'Edit dictionary item');
		$this->render('add');
	}
	

}

?>